# -*- coding: utf-8 -*-

# 精灵序列帧例子

import sys, pygame, random, math
from pygame.locals import *
import time as t
from datetime import datetime, date, time

reload(sys)
sys.setdefaultencoding('utf-8')


class MySprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.master_image = None
        self.frame = 0
        self.old_frame = -1
        self.frame_width = 1
        self.frame_height = 1
        self.first_frame = 0
        self.last_frame = 0
        self.columns = 1
        self.last_time = 0

    def _getx(self): return self.rect.x
    def _setx(self, value): self.rect.x = value
    X = property(_getx, _setx)

    def _gety(self): return self.rect.y
    def _sety(self, value): self.rect.y = value
    Y = property(_gety, _sety)

    def _getpos(self): return self.rect.topleft
    def _setpos(self, pos): self.rect.topleft = pos
    position = property(_getpos, _setpos)

    def load(self, filename, width, height, columns):
        self.master_image = pygame.image.load(filename).convert_alpha()
        self.frame_width = width
        self.frame_height = height
        self.rect = Rect(0,0,width,height)
        self.columns = columns
        rect = self.master_image.get_rect()
        self.last_frame = (rect.width//width)*(rect.height//height)-1

    def update(self, current_time, rate=100):
        if current_time > self.last_time + rate:
            self.frame += 1
            if self.frame > self.last_frame:
                self.frame = self.first_frame
            self.last_time = current_time

        if self.frame != self.old_frame:
            frame_x = (self.frame%self.columns)*self.frame_width
            frame_y = (self.frame//self.columns)*self.frame_height
            rect = Rect(frame_x, frame_y, self.frame_width, self.frame_height)
            self.image = self.master_image.subsurface(rect)
            self.old_frame = self.frame

    def __str__(self):
        return str(self.frame)+","+str(self.first_frame)+\
            ","+str(self.last_time)+","+str(self.frame_width)+\
            ","+str(self.frame_height)+","+str(self.columns)+\
            ","+str(self.rect)



def print_text(font, x, y, text, color=(255,255,255), shadow=False):
    if shadow:
        imgText = font.render(text, True, (0, 0, 0))
        screen.blit(imgText, (x - 2, y - 2))
    image = font.render(text, True, color)
    screen.blit(image, (x, y))


def wrap_angle(angle):
    return abs(angle %360)

pygame.init()
winstyle = 0  # |FULLSCREEN
bestdepth = pygame.display.mode_ok((800, 600), winstyle, 32)
screen = pygame.display.set_mode((800, 600), winstyle, bestdepth)

pygame.display.set_caption("精灵序列帧例子")
font1 = pygame.font.Font("freesansbold.ttf", 10)
framerate = pygame.time.Clock()


dragon = MySprite()
dragon.load("dragon.png", 164, 124, 2)

group = pygame.sprite.Group()
group.add(dragon)

while True:
    framerate.tick(30)
    ticks = pygame.time.get_ticks()

    for event in pygame.event.get():
        if event.type == QUIT: sys.exit()

    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]: sys.exit()

    screen.fill((0, 0, 100))

    group.update(ticks)
    group.draw(screen)
    print_text(font1, 0, 0, "Sprite: " + str(dragon))

    pygame.display.update()



